--
-- Author: yawei.zhu
-- Date: 2018-09-17 16:43:24
--
-- DynastyMapController
--

local DynastyTopView = import("..views.DynastyTopView")
local DynastyRightView = import("..views.DynastyRightView")
local DynastyMiniMapPanel = import("..views.renders.DynastyMiniMapPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("DynastyMapModel")
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
end

function ClassRef:onEvent(eventName, data, data2)
    if eventName == "onMapChanged" then
        -- 移除旧的UI
        if self._miniMap and not cc.isDead(self._miniMap) then
            self._miniMap:removeFromParent()
        end
        self._miniMap = nil

        if self._topView and not cc.isDead(self._topView) then 
            self._topView:removeFromParent()
        end
        self._topView = nil

        if self._rightView and not cc.isDead(self._rightView) then 
            self._rightView:removeFromParent()
        end
        self._rightView = nil

        if self._model:isDynastyMap() then
            self._strongholdInfo = GD:getStrongholdInfoByMapId(MapManager:getMapId())
            self._tonwId = tonumber(self._strongholdInfo.districtID) -- 当前领地ID
            self._strongholdId = tonumber(self._strongholdInfo.ID) -- 当前据点ID
            self._model:reqDynastyInfo(self._tonwId)
        else
            self._strongholdId = nil
            self._tonwId = 0
            self._setHoldId = nil
        end
    elseif eventName == "updateMyCharacterPos" then
        self:updateCharcterPosX(data)
    elseif eventName == "updateOtherCharacterPos" then
        self:updateOtherCharcterPosX(data.uid, data.posX)
    end
end

function ClassRef:listEvent()
    return {
        "onMapChanged",
        "updateMyCharacterPos",
        "updateOtherCharacterPos"
    }
end

-- 显示小地图（把Node当作Panel来处理，不能当作view，位置设置上会有各种bug）
function ClassRef:showMiniMap(townInfo)
    if not self._miniMap or cc.isDead(self._miniMap) then
        local panel = DynastyMiniMapPanel.create()
        panel:setPosition(cc.p(display.width / 2 , display.height))
        self:addChild(panel, {isFade = false})

        self._miniMap = panel
        -- 刚进地图时刷新一次我的位置
        local character = FieldManager:getMyCharacter()
        self:updateCharcterPosX(character:getPositionX())
    end
    self._miniMap:updateView(townInfo, UD:getGuildId())
end

function ClassRef:updateCharcterPosX(posX)
    if cc.isDead(self._miniMap) then return end
    self._miniMap:updateCharcterPosX(posX)
end

function ClassRef:updateOtherCharcterPosX(uid, PosX)
    if cc.isDead(self._miniMap) then return end
    self._miniMap:updateOtherCharcterPosX(uid, PosX)
end

-- 显示上方view
function ClassRef:showTopView(townInfo)
    if not self._topView or cc.isDead(self._topView) then
        local view = DynastyTopView.new()
        view:setPosition(cc.p( display.safe_width / 2 , display.height ))
        view:onCloseView(function() self:removeChild(view, {isFade = false}) end)
        view:onOperate(handler(self, self.onOperate))
        self:addChild(view, {isFade = false} )
        self._topView = view
    end
    self._topView:updateView(townInfo, UD:getGuildId())
end

-- 显示右方view
function ClassRef:showRightView(townInfo)
    if not self._rightView or cc.isDead(self._rightView) then
        local view = DynastyRightView.new()
        view:onCloseView(function() self:removeChild(view, {isFade = false}) end)
        view:onOperate(handler(self, self.onOperate))
        view:setPosition(cc.p(display.width, 520))
        view:setContentSize(cc.size(0, 0)) -- 必须要加，否则会盖到其他UI
        self:addChild(view, {isFade = false} )
        self._rightView = view
    end
    -- 设置位置
    local desktopController = UIHandler:getDesktopController()
    if desktopController then
        local desktop = desktopController:getDesktop()
        if desktop then
            local bossNode = desktop:getBossNode()
            self._rightView:setPosition(bossNode:getPosition())
        end
    end

    -- 默认显示当前据点
    local holdIndex = 1
    if self._strongholdId then
        holdIndex = townInfo:getIndexByStrongholdId(self._strongholdId)
    end
    
    self._rightView:updateView(townInfo, holdIndex, UD:getGuildId())
end


-------------------------------------------------------
function ClassRef:showDynastyMapView()
    if not cc.isDead(self._miniMap) then
        self._miniMap:setVisible(true)
    end
    if not cc.isDead(self._topView) then
        self._topView:setVisible(true)
    end
    if not cc.isDead(self._rightView) then
        self._rightView:setVisible(true)
    end
end

function ClassRef:hideDynastyMapView()
    if not cc.isDead(self._miniMap) then
        self._miniMap:setVisible(false)
    end
    if not cc.isDead(self._topView) then
        self._topView:setVisible(false)
    end
    if not cc.isDead(self._rightView) then
        self._rightView:setVisible(false)
    end
end

-- 设置我的队伍的人数
function ClassRef:setMyTeamCount(num)
    if cc.isDead(self._miniMap) then return end
    self._miniMap:setMyTeamCount(num)
end

-- 添加其他队伍的玩家
function ClassRef:insertOtherTeamMember(leaderId, isEnemy, posX)
    if cc.isDead(self._miniMap) then return end
    self._miniMap:insertOtherTeamMember(leaderId, isEnemy, posX)
end

function ClassRef:removeAllTeamNodes()
    if cc.isDead(self._miniMap) then return end
    self._miniMap:removeAllTeamNodes()
end

-------------------------------------------------------
function ClassRef:onOperate(op, data)
    print("DynastyMapController:onOperate op = " .. op)
    if op == "reqDynastyTownInfo" then
        self._model:reqDynastyInfo(data)
    elseif op == "changeStronghold" then
        app:sendMsg("DynastyBattleController", "openView", data)
    elseif op == "showCountDonw" then
        EM:notify("onNormalCountDown",{second = data, formatStr = L("champion_battle7")})
    end
end

return ClassRef
